package com.runda.biz.controller;

import java.util.List;

import javax.annotation.Resource;

import com.bbjob.dto.Result;
import com.bbjob.model.Production;
import com.bbjob.model.ProductionCodes;
import com.runda.biz.dto.MutiCodesDto;
import com.runda.biz.service.IProductCodeService;
import com.runda.biz.service.IProductionService;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController
@RequestMapping("production/code")
public class ProductionCodesController{
    @Resource
    private IProductCodeService productionCodeService;
    @Resource
    private IProductionService productService;
    @GetMapping("{codeType}/list/{productId}")
    public Result<List<ProductionCodes>> getList(@PathVariable("codeType") String codeType,@PathVariable("productId")Integer productionId){
      ProductionCodes code=new ProductionCodes();
      code.setCodeType(codeType);
      code.setProductId(productionId);
      List<ProductionCodes> list= productionCodeService.selectByRecord(code);
      return new Result<List<ProductionCodes>>(list);
    }
    @GetMapping("list/{productId}")
    public Result<List<ProductionCodes>> getList(@PathVariable("productId")Integer productionId){
      List<ProductionCodes> list= productionCodeService.selectByProperty("productId", productionId, ProductionCodes.class);
      return new Result<List<ProductionCodes>>(list);
    }
    @PostMapping("save/{productId}/list")
    public Result<String> addProductionCodes(@PathVariable("productId") Integer productId,@RequestBody MutiCodesDto code){
       int ok= this.productionCodeService.save(code,productId);
       if(ok>0)
         return new Result<String>();
       return new Result<String>(5000,"未知原因保存失败,请联系管理员");
    }
    @GetMapping(value="valid/{code}")
    public Result<String> isExistCode(@PathVariable("code") String code) {
      ProductionCodes record=new ProductionCodes();
      record.setCode(code);
      ProductionCodes productCode = this.productionCodeService.selectByOne(record);
      if(productCode!=null){
         Production product=this.productService.selectByKey(productCode.getProductId());
         if(product!=null){
            return new Result<String>(5001, "检测到恒安No:"+product.getHengAnNo()+"的"+productCode.getCodeType()+"中存在相同编号");
         }
      }
      return new Result<>();
    }
    @GetMapping(value="del/{code}")
    public Result<String> getMethodName(@PathVariable("code") String code) {
      ProductionCodes pcode=new ProductionCodes();
      pcode.setCode(code);
      this.productionCodeService.delBy(pcode);
      return new Result<String>();
    }
    @PostMapping("save")
    public Result<String> addProductionCode(@RequestBody ProductionCodes code){
      int ok= this.productionCodeService.save(code);
      if(ok>0)
        return new Result<String>();
      return new Result<String>(5000,"未知原因保存失败,请联系管理员");
   }
    
}